import * as THREE from "../three/build/three.module.js";
import {GLTFLoader} from "../three/examples/jsm/loaders/GLTFLoader.js";
// import {OrbitControls} from "../three/examples/jsm/controls/OrbitControls.js";

let renderer, camera, scene;
let cameraPositions=[
    {   
        pos:new THREE.Vector3(-4.948270676701784, 12.016218675975834, -3.1259594751547075),
        rot:new THREE.Vector3(-1.841207230065056, -0.41127707249472323, -2.1770671031878837)
    },{
        pos:new THREE.Vector3(20.662233921735996, 12.526418376404614, -6.139373234703496),
        rot:new THREE.Vector3(-1.841207230065056, -0.41127707249472323, -2.1770671031878837)
    },{
        pos:new THREE.Vector3(26.515902941416226, 12.526418376404614, -8.764207685085553),
        rot:new THREE.Vector3(-1.841207230065056, -0.41127707249472323, -2.1770671031878837)
    },{
        pos:new THREE.Vector3(24.682185384779597, 14.104109549283162, 21.497764496493254),
        rot:new THREE.Vector3(-1.841207230065056, -0.41127707249472323, -2.1770671031878837)
    },{
        pos:new THREE.Vector3(-9.365038387839322, 14.104109549283162, -7.005216140945471),
        rot:new THREE.Vector3(-1.841207230065056, -0.41127707249472323, -2.1770671031878837)
    },{
        pos:new THREE.Vector3(-6.973808892693906, 8.30003243665689, 3.2427190275947484),
        rot:new THREE.Vector3(-1.841207230065056, -0.41127707249472323, -2.1770671031878837)
    },{
        pos:new THREE.Vector3(-11.067562072623478, 8.30003243665689, 1.8604588577473922),
        rot:new THREE.Vector3(-1.841207230065056, -0.41127707249472323, -2.1770671031878837)
    }
];
let timeMax = 1000;
let time = timeMax; // 计时缓动用
let oldPos;
let newPos;

window.cameraPositions = cameraPositions;
window.cameraMove = cameraMove;

function init(){
    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera(50, window.innerWidth/ window.innerHeight, 0.01, 1000);
    renderer = new THREE.WebGLRenderer({ antialias: true, logarithmicDepthBuffer: true });
    camera.position.set( 1.9700559369164656, 8.30003243665689, 25.037129121023394);
    camera.rotation.x = -1.841207230065056;
    camera.rotation.y = -0.41127707249472323;
    camera.rotation.z = -2.1770671031878837;

    renderer.setSize(window.innerWidth, window.innerHeight);
    // renderer.setClearColor(0x00ff00);
    document.body.appendChild(renderer.domElement);

    // new OrbitControls(camera,renderer.domElement);

    window.scene = scene;
    window.camera = camera;
    window.renderer = renderer;
}
function animate(){
    requestAnimationFrame(animate);
    next();
    renderer.render(scene, camera);
}
function next(){
    time ++;
    if(time>timeMax)
        return;

    let newP = newPos.clone();
    newP.sub(oldPos);
    newP.multiplyScalar(time/timeMax);
    newP.add(oldPos);
    cameraMove({pos:newP ,rot:cameraPositions[0].rot} );
}
function loadScene(){
    let cube = new THREE.Mesh(
        new THREE.BoxBufferGeometry(), 
        new THREE.MeshBasicMaterial({color:0x00ff00}));
    cube.position.set( 4.970055936916466, 1.30003243665689, 27.037129121023394);
    window.cube=cube;
    scene.add(cube);
    let lightA = new THREE.AmbientLight(0xffffff);
    scene.add(lightA);
    let loader = new GLTFLoader();
    loader.load('./ttt1.gltf',function(gltf){
        scene.add(gltf.scene);
    })
}

function cameraMove(posData){
    camera.position.copy(posData.pos);
    camera.rotation.x = posData.rot.x;
    camera.rotation.y = posData.rot.y;
    camera.rotation.z = posData.rot.z;
}
function UI(){
    for(let i=0; i < cameraPositions.length; i++){
        let domEle = document.getElementById("b"+i);
        domEle.onclick = function s(){
            time = 0;
            oldPos = camera.position;
            newPos = cameraPositions[i].pos;
            // cameraMove(cameraPositions[i]);
        };
    }
}

init();
loadScene();
animate();

window.onload = UI;